#Ni Terminal v1.0.0
#Copyright 2023 Five Studio.io
#按照Apchce-2.0方式开源
#-------------------------------
from os import system as kernel,chdir,getcwd
from getpass import getuser
from socket import gethostname
#全局预定义变量
extpakls,pakls=[],[]   #外部包列表,内部包列表
#获取主机名/用户名模块
def get_hous (info):
    if info == 'host':
        #获取主机名
        return gethostname()
    elif info == 'user':
        #获取用户名
        return getuser()
#更改或查看当前设置路径
def gech_path (info,cpath=chdir()):
    if info == 'get':
        #查看当前路径
        return getcwd()
    elif info == 'change':
        #更改当前路径
        chdir(cpath)
        return 0
#自定义终端提示内容
def get_tinfo ():
    tinfo_ls=[]
    tinfo_ls.append(get_hous('user')+'@'+\
        get_hous('host')+' NiT '+gech_path('get'))
    tinfo_ls.append('$')
    return tinfo_ls
#内部附加功能
#指令分割
def codefg (info,rcode):
    #指令节选-名称
    if rcode == 'name':
        repname=info.replace(' ','|')
        pname,temp='',''
        for i in repname:
            if i == '|':
                temp=info.replace(pname,'')
                break
            pname=pname + i
        return pname
    #指令节选-参数
    elif rcode == 'cs':
        repname=info.replace(' ','|')
        pname,temp='',''
        for i in repname:
            if i == '|':
                temp=info.replace(pname,'')
                break
        temp=temp.split('-')
        temp1=[]
        for i in temp:
            temp1.append(i.replace(' ',''))
        return temp1
    '''
    变量解释
    pname String 包名称
    csls  List   参数列表
    '''
def runewpak (runpakname):
    pname=codefg(runpakname,'name')
    cs=codefg(runpakname,'cs')
    if not (pname in pakls):
        return -1
    #预先定义区域-开始
    def installpakgn (cks):
        temp=cks.replace('install ','')
        errcode=insextpak(temp)
        if errcode == -1:
            print('错误,没有找到名为{}.eap的包文件'.format(temp))
            return
        print('{}.eap包文件装载完成.'.format(temp))
        exec('{}_setup()'.format(temp),globals())
    def unistallpakgn (cks):
        temp=cks.replace('unistall ','')
        if not (temp in extpakls):
            print('错误,当前没有装载名为{}的外部包'.format(temp))
            return
        unisextpak(temp)
        exec('{}_unis()'.format(temp),globals())
        print('{}.eap包文件卸载成功.'.format(temp))
    #预先定义区域-结束
    #识别判断+运行
    #----特殊! 外部包运行程序
    if 'install' in runpakname:
       installpakgn(runpakname)
    elif 'unisatll' in runpakname:
        unistallpakgn(runpakname)
    temp=0
    for i in extpakls:
        if runpakname in i:
            runextpak(codefg(runpakname,'name'),codefg(runpakname,'cs'))
            break
            temp=1
    if temp == 0:
        print('未找到于{}相关的任何包!'.format(codefg(runpakname,'name')))    
#外部包-装载
#模块名(全名):Install External Pak -> insextpak
def insextpak (extpakname):
    try:
        epakf=open('./ExtAppliction/{}.eap'.format(extpakname),'r')
    except:
        return -1
    epakf_code=epakf.read()
    exec(epakf_code,globals())
    extpakls.append(extpakname)
#外部包-运行
def runextpak (cks=''):
    extpakname=codefg(cks,'name')
    cks=codefg(cks,'cs')
    if not (extpakname in extpakls):
        return -1
    temp=codefg(cks,'cs')
    runcode=extpakname+' ({})'.format(str(temp))
    exec(runcode,globals())
#外部包-卸载
def unisextpak (unispakname):
    if not (unispakname in extpakls):
        return -1
    extpakls.remove(unispakname)
#启动时页面
def bootshow ():
    kernel('color 01')
    print('~{:^120}'.format('Welcome to Ni Terminal Vision 1.0.0!'))
    print('~{:^120}'.format('Copyright 2023 Five Studio.io'))
    print('~{:^120}'.format('Dong Xuchen. 设计制做'))
    print('~{:^120}'.format('这个项目使用Apchce-2.0开源'))
    print('~\n'*9,end='')
    bootsl=input('>')
    if bootsl == '':
        return 0
    else:
        return bootsl
#命令识别与执行
